

package jorm;

import jorm.dialect.Dialect;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * ActiveRecord plugin.
 * 
 * ActiveRecord plugin not support mysql type year, you can use int instead of year. 
 * Mysql error message for type year when insert a record: Data truncated for column 'xxx' at row 1
 */
public class ActiveRecordPlugin implements IPlugin {

	private static boolean isStarted = false;
	private static DataSource dataSource;
	private static IDataSourceProvider dataSourceProvider;
	private static final List<TableInfo> tableMappings = new ArrayList<TableInfo>();


    /**
     * 第三方插件配置数据源
     * @param dataSourceProvider
     */
    public ActiveRecordPlugin(IDataSourceProvider dataSourceProvider) {
        ActiveRecordPlugin.dataSourceProvider = dataSourceProvider;
        start();

    }
    /**
     * 第三方插件配置数据源
     * @param dataSourceProvider
     * @param   transactionLevel 事务支持等级
     */
    public ActiveRecordPlugin(IDataSourceProvider dataSourceProvider, int transactionLevel) {
       ActiveRecordPlugin.dataSourceProvider = dataSourceProvider;
        DbKit.setTransactionLevel(transactionLevel);
        start();
    }

    /**
     * 指定数据源
     * @param dataSource
     */
    public ActiveRecordPlugin(DataSource dataSource) {
       ActiveRecordPlugin.dataSource = dataSource;
        start();
    }

    public ActiveRecordPlugin(DataSource dataSource, int transactionLevel) {

      ActiveRecordPlugin.dataSource = dataSource;
        DbKit.setTransactionLevel(transactionLevel);
        start();
    }

	public static void setDevMode(boolean devMode) {
		DbKit.setDevMode(devMode);
	}


    /**
     * 指定dialect eg:mysqlDB --- setDialect(new MysqlDialect（）)
     * @param dialect
     * @return
     */
	public ActiveRecordPlugin setDialect(Dialect dialect) {
		if (dialect != null)
			DbKit.setDialect(dialect);
		return this;
	}

    /**
     * 是否显示执行的sql语句  debug
     * @param showSql
     * @return
     */
	public ActiveRecordPlugin setShowSql(boolean showSql) {
		DbKit.setShowSql(showSql);
		return this;
	}

	public ActiveRecordPlugin setContainerFactory(IContainerFactory containerFactory) {
		DbKit.setContainerFactory(containerFactory);
		return this;
	}



    /**
     * 注册映射model关系
     * @param tableName  表名
     * @param primaryKey 主键name
     * @param modelClass 映射的model class
     * @return
     */
	public ActiveRecordPlugin addMapping(String tableName, String primaryKey, Class<? extends Model<?>> modelClass) {
		tableMappings.add(new TableInfo(tableName, primaryKey, modelClass));
		return this;
	}

    /**
     * 注册映射model关系
     * @param tableName  表名
     * @param modelClass 映射的model class
     * @return
     */
	public ActiveRecordPlugin addMapping(String tableName, Class<? extends Model<?>> modelClass) {
		tableMappings.add(new TableInfo(tableName, modelClass));
		return this;
	}

	public boolean start() {
		if (isStarted)
			return true;
		if (dataSourceProvider != null)
			dataSource = dataSourceProvider.getDataSource();

		if (dataSource == null)
			throw new RuntimeException("ActiveRecord start error: ActiveRecordPlugin need DataSource or DataSourceProvider");

		DbKit.setDataSource(dataSource);

		isStarted = true;
		return isStarted;
	}



	public boolean stop() {
		isStarted = false;
		return true;
	}

    /**
     * 注册modl
     *
     * @return
     */
    @Override
    public boolean register(){
        return TableInfoBuilder.buildTableInfo(tableMappings);  //To change body of implemented methods use File | Settings | File Templates.
    }
}
